//
//  ViewController.m
//  news_ios_app
//
//  Created by apple on 16/3/15.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ViewController.h"
#import "HttpRequestManager.h"
#import "loginUserModel.h"
#import <ImageIO/ImageIO.h>

@interface ViewController ()
@property (strong,nonatomic) NSMutableArray *newsYpyeArr;

@property (nonatomic,strong) YALSunnyRefreshControl *sunnyRefreshControl;

@property (nonatomic,strong) NSTimer *timer;//模拟数据刷新需要的时间控制器

@property (nonatomic,assign) int time;

-(void)loadNewData;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    _newsYpyeArr = [NSMutableArray array];
    
    

    
    
//    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
//    [parameter setObject:@"6231" forKey:@"article_id"];
//    [HttpRequestManager HttpRequestForArticleDetailByIdWithParameter:parameter success:^(id data) {
//        if ([[data objectForKey:@"message"] isEqualToString:@"success"]) {
//            
//            
//            ArticleDetailModel *articleDetailModel = [ArticleDetailModel modelWithDictionary:[data objectForKey:@"result"]];
//            
//            UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//            [self.view addSubview:webView];
//                
//            [webView loadHTMLString:articleDetailModel.content baseURL:nil];
//
//        }else{
//            NSLog(@"请求失败");
//        }
//    } failure:nil];
    
    
//    //插入数据
//    BmobObject *user = [BmobObject objectWithClassName:@"_User"];
//    loginUserModel * model = [loginUserModel new];
//    model.username = @"wang";
//    model.password = @"123";
//    model.email = @"514335620@qq.com";
//    model.mobilePhoneNumber = @"13851901775";
//    
//    [user saveAllWithDictionary:[model modelToDict]];
//    [user saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
//        //进行操作
//        if (isSuccessful) {
//            NSLog(@"数据库插入成功");
//        }
//    }];
//    
//    //创建BmobQuery实例，指定对应要操作的数据表名称
//    BmobQuery *query = [BmobQuery queryWithClassName:@"_User"];
//    //按updatedAt进行降序排列
//    [query orderByDescending:@"updatedAt"];
//    //返回最多20个结果
//    query.limit = 20;
//    //执行查询
//    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
//        //处理查询结果
//        for (BmobObject *obj in array) {
//             loginUserModel * model = [loginUserModel new];
//            model.username = [obj objectForKey:@"username"];
//            if ([model.username isEqualToString:@"wang"]) {
//                NSLog(@"找到了wang,密码是%@",[obj objectForKey:@"password"]);
//            }
//        }
//    }];

    self.view.backgroundColor = [UIColor whiteColor];
    self.sunnyRefreshControl =[YALSunnyRefreshControl attachToScrollView:self.baseTableView target:self refreshAction:@selector(loadNewData)];

    
//    [self showWaitAlert];
    
    
    [self.baseTableView registerClass:[wangdaTestCell class] forCellReuseIdentifier:[ArticleDetailModel cellType]];
    ArticleDetailModel *wang1 = [[ArticleDetailModel alloc]init];
    wang1.content = @"wang1";
    ArticleDetailModel *wang2 = [[ArticleDetailModel alloc]init];
    wang2.content = @"wang2";
    NSMutableArray * data = [[NSMutableArray alloc]init];
    [data addObject:wang1];
    [data addObject:wang2];
    [self.baseTableView setFrame:CGRectMake(0, 100, VIEWWIDTH ,VIEWHEIGHT )];
    [self.view addSubview:self.baseTableView];
    
    [self setDataArr:data];
    [self.baseTableView reloadData];
}

- (NSMutableArray *)dealWithImageStr:(NSString *)str
{
    NSMutableArray * imageArr = [NSMutableArray array];
    NSURL *url = [[NSBundle mainBundle] URLForResource:str withExtension:nil];
    CGImageSourceRef csf = CGImageSourceCreateWithURL((__bridge CFTypeRef) url, NULL);
    size_t const count = CGImageSourceGetCount(csf);
    CGImageRef images[count];
    for (size_t i = 0; i < count; ++i) {
        images[i] = CGImageSourceCreateImageAtIndex(csf, i, NULL);
        UIImage *image =[[UIImage alloc] initWithCGImage:images[i]];
        [imageArr addObject:image];
    }
    return imageArr;
}

-(void)loadNewData {
    //模拟刷新的时间
    self.timer  =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeAction) userInfo:nil repeats:NO];
    self.time = 3;
}
-(void)timeAction {
    self.time --;
    NSLog(@"%d",self.time);
    if (self.time <= 0) {
        //		刷新数据
        [self.baseTableView reloadData];
        //		停止刷新
        [self.sunnyRefreshControl endRefreshing];
        [self.timer invalidate];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


@interface wangdaTestCell ()

@end

@implementation wangdaTestCell

- (instancetype)init
{
    if (self == [super init]) {
//        [self setDelegate:self];
    }
    return self;
}

- (void)updateCellWithModel:(id)model
{
    if (![model isKindOfClass:[ArticleDetailModel class]]) {
        return;
    }
    ArticleDetailModel *cellModel = (ArticleDetailModel *)model;
    [self.textLabel setText:cellModel.content];
}

- (CGFloat)heightWithModel:(id)model
{
    if (![model isKindOfClass:[ArticleDetailModel class]]) {
        return 0;
    }
    return 50.0f;
}

//+ (CGFloat)getHeightWithModel:(id)model
//{
//    return [[self class] heightWithModel:model];
//}

@end


